<template>
  <a-modal
    :open="open"
    width="720px"
    :title="rowData?.id ? '修改候诊区' : '新增候诊区'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form class="form-style-flex" name="validate_other" :model="formState" :label-col="{ style: { width: '120px' } }">
      <a-form-item label="候诊区编号" class="w-1/2" v-bind="validateInfos.regionId">
        <a-input v-model:value="formState.regionId" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="候诊区名称" class="w-1/2" v-bind="validateInfos.regionName">
        <a-input v-model:value="formState.regionName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="主屏显地址端口" class="w-1/2" v-bind="validateInfos.screenAddr">
        <a-input v-model:value="formState.screenAddr" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="语音地址端口" class="w-1/2" v-bind="validateInfos.speechAddr">
        <a-input v-model:value="formState.speechAddr" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="关联科室" class="w-full" v-bind="validateInfos.deptCode">
        <SelectSection v-model:value="formState.deptCode" multiple />
      </a-form-item>
      <a-form-item label="滚动信息" class="w-full">
        <a-input v-model:value="formState.scrollInfo" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="预分配人数" class="w-full">
        <a-input v-model:value="formState.preArrangeCount" v-input:int placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.cmemo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'update:value', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  },
  title: {
    type: String,
    default: ''
  }
})

const formState = ref({
  regionId: '',
  regionName: '',
  screenAddr: '',
  speechAddr: '',
  deptCode: [],
  scrollInfo: '',
  preArrangeCount: '',
  cmemo: ''
})

const rulesState = ref({
  regionId: [{ required: true, message: '请输入候诊区编号' }],
  regionName: [{ required: true, message: '请输入候诊区名称' }],
  screenAddr: [{ required: true, message: '请输入主屏显地址端口' }],
  speechAddr: [{ required: true, message: '请输入语音地址端口' }],
  deptCode: [{ required: true, message: '请选择关联科室' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData?.id) {
        formState.value = {
          ...props.rowData,
          deptCode: props.rowData.deptCode.split(',')
        }
      } else {
        formState.value = {
          regionId: '',
          regionName: '',
          screenAddr: '',
          speechAddr: '',
          deptCode: [],
          scrollInfo: '',
          preArrangeCount: '',
          cmemo: ''
        }
      }
    }
  }
)

const onOk = async () => {
  await validate()
  if (!formState.value.deptCode || formState.value.deptCode.length <= 0) return message.warning('请选择关联科室')
  const res = await post('/outp/dictRegion/doSaveRegion', {
    ...formState.value,
    deptCode: formState.value.deptCode.join(',')
  })
  if (res.code !== 0) return message.error(res.msg || '保存失败')
  message.success(res.msg || '保存成功')
  emit('success')
  onCancel()
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
</script>
<style lang="less" scoped></style>
